package top.ucat.x.service.sso.server.service.provider;

import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import top.ucat.box.cloud.common.exception.BaseException;
import top.ucat.box.starter.oauth.common.bean.entity.OauthUserCredentials;
import top.ucat.box.starter.oauth.common.bean.oauth.UserCredentialBo;
import top.ucat.x.sso.service.provider.UserService;
import top.ucat.x.system.entity.SysUser;
import top.ucat.x.system.service.provider.SystemUserService;
import top.ucat.x.uaa.service.bean.SystemUser;

import java.util.HashSet;

/**
 * @Auther: Jun
 * @Date: 2020/4/3 12:08
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    @Reference
    private SystemUserService systemUserService;

    @Override
    public SystemUser getUser(UserCredentialBo credentialBo) throws UsernameNotFoundException {

        SysUser sysUser = systemUserService.getUserByUserCode(credentialBo.getUserCode());
        if (sysUser == null) {
            throw new BaseException("找不到此用户", 400);
        }
        SystemUser user = SystemUser.builder()
                .user(sysUser)
                .username(credentialBo.getUserCode())
                .password(sysUser.getPassword())
                .permissions(new HashSet())
                .build();
        return user;
    }
}
